package com.chengh.nami.web.controller.system;

import com.chengh.nami.common.exception.BizException;
import com.chengh.nami.common.request.PageQuery;
import com.chengh.nami.common.response.PageData;
import com.chengh.nami.common.response.R;
import com.chengh.nami.common.utils.FileUtils;
import com.chengh.nami.common.utils.StringUtils;
import com.chengh.nami.system.domain.dto.DictTypeDto;
import com.chengh.nami.system.pojo.SysDictType;
import com.chengh.nami.system.service.ISysDictTypeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Tag(name = "SysDictTypeController", description = "字典类型管理")
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/system/dict/type")
public class SysDictTypeController {

    private final ISysDictTypeService dictTypeService;

    /**
     * 获取字典类型列表
     *
     * @param dictTypeDto 查询参数
     * @param pageQuery   分页信息
     * @return 字典类型列表
     */
    @GetMapping()
    @Operation(summary = "获取字典类型列表", description = "")
    public R<PageData<SysDictType>> list(DictTypeDto dictTypeDto, PageQuery pageQuery) {
        PageData<SysDictType> pageDict = dictTypeService.getListType(dictTypeDto, pageQuery);
        return R.ok(pageDict);
    }

    /**
     * 根绝Id获取字典类型信息
     *
     * @param id 字典类型ID
     * @return 字典类型信息
     */
    @GetMapping("/{id}")
    @Operation(summary = "根绝Id获取字典类型信息", description = "")
    public R<SysDictType> getInfo(@PathVariable Long id) {
        return R.ok(dictTypeService.getById(id));
    }

    /**
     * 修改字典类型信息
     *
     * @param dictType 字典类型信息
     * @return 是否成功
     */
    @PutMapping()
    @Operation(summary = "修改字典信息", description = "")
    public R<Void> editDict(@RequestBody SysDictType dictType) {
        verifyDictType(dictType);
        return R.bool(dictTypeService.edit(dictType));
    }


    /**
     * 添加字典类型信息
     *
     * @param sysDictType 字典类型信息
     * @return 是否成功
     */
    @PostMapping()
    @Operation(summary = "添加字典类型", description = "")
    public R<Void> addDict(@RequestBody SysDictType sysDictType) {
        verifyDictType(sysDictType);
        return R.bool(dictTypeService.addDict(sysDictType));
    }

    /**
     * 删除字典类型信息
     *
     * @param ids 字典类型ID信息
     * @return 是否成功
     */
    @DeleteMapping("/{ids}")
    @Operation(summary = "删除字典信息", description = "")
    public R<Void> delDict(@PathVariable Long[] ids) {
        dictTypeService.delDict(ids);
        return R.ok();
    }

    @DeleteMapping("/refreshCache")
    @Operation(summary = "刷新缓存", description = "")
    public R<Void> refreshCache() {
        dictTypeService.refreshCache();
        return R.ok();
    }

    @PostMapping("/export")
    @Operation(summary = "导出数据", description = "")
    public void export(HttpServletResponse response, DictTypeDto dictTypeDto) throws IOException {
        List<SysDictType> list = dictTypeService.getList(dictTypeDto);
        List<Map<String, Object>> exprotList = new ArrayList<>();
        for (SysDictType sysDictType : list) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("字典编号", sysDictType.getId());
            map.put("字典名称", sysDictType.getDictName());
            map.put("字典类型", sysDictType.getDictType());
            map.put("备注", sysDictType.getRemark());
            map.put("创建时间", sysDictType.getCreateTime());
            exprotList.add(map);
        }
        FileUtils.downloadExcel(exprotList, response);
    }

    /**
     * 验证字典信息
     *
     * @param dictType 字典类型信息
     */
    public static void verifyDictType(SysDictType dictType) {
        if (StringUtils.isEmpty(dictType.getDictType())) {
            throw new BizException("请求的字典类型不能为空");
        }
        if (StringUtils.isEmpty(dictType.getDictName())) {
            throw new BizException("请求的字典名称不饿能为空");
        }
    }
}
